package com.aasa.siccosap.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.aasa.siccosap.bean.GarantiaPorLinea;
import com.aasa.siccosap.bean.ParametrosGenerales;
import com.aasa.siccosap.util.Constantes;

public class GarantiaPorLineaDAO extends ConsultaClienteBaseDAO{

	
	static Logger logger = Logger.getLogger("GarantiaPorLineaDAO.class");
	public GarantiaPorLineaDAO()	
	throws accesoDaoException {
		super();
	}
	
	public GarantiaPorLineaDAO(DataSource dataSource) {
		super(dataSource);
	}
	
	
	public List<GarantiaPorLinea> consultarGarantiasPorLinea(ParametrosGenerales parametros) 
	throws Exception{
        ResultSet rs = null;
        this.av_message = null;
        this.storedProcedure = "begin CONSULTA_GARANTIAS_PKG.P_GARANTIAS_LINEAS(?,?,?); end;";
        
        List<GarantiaPorLinea> aObjL_consultaGarantiasPorLinea=new ArrayList<GarantiaPorLinea>();
        
        try{
                this.conn = dataSource.getConnection();
                this.stmt = this.conn.prepareCall(storedProcedure);                              
                configuraParametrosConsultaClienteGenerales(parametros);
                
                this.stmt.execute();
                this.av_message = (String)stmt.getObject(1);                	
                    if(this.av_message.trim().toUpperCase().endsWith(Constantes.NO_ERRORS)){
                        
                        rs = (ResultSet)stmt.getObject(2);
                        while(rs.next()){
                        	GarantiaPorLinea objL_garantia = creaRegistroGarantiaPorLinea(rs);
                        	aObjL_consultaGarantiasPorLinea.add(objL_garantia);
                        }
                }else{
                    return null;
                }

        }catch (Exception e){
                e.printStackTrace();
                return null;
        }finally{
        		close(this.conn, rs, stmt);
        }
        return(aObjL_consultaGarantiasPorLinea);
        
    }

	private GarantiaPorLinea creaRegistroGarantiaPorLinea(ResultSet objA_resultSet) throws SQLException {
		GarantiaPorLinea objL_garantiaPorLinea = new GarantiaPorLinea();
		
		objL_garantiaPorLinea.setStrPb_codigoLinea(objA_resultSet.getString("COD_LINGARA"));
		objL_garantiaPorLinea.setObjPb_fechaVencimiento(objA_resultSet.getDate("FEC_VEN_GAR"));
		objL_garantiaPorLinea.setDblPb_montoDespacho(objA_resultSet.getDouble("TOT_DESPACHO_DOL"));
		objL_garantiaPorLinea.setDblPb_montoDisponible(objA_resultSet.getDouble("DISPONIBLE_DOL"));
		objL_garantiaPorLinea.setDblPb_montoLineaCredito(objA_resultSet.getDouble("IMPORTE_DOL"));
		objL_garantiaPorLinea.setDblPb_montoNoProc(objA_resultSet.getDouble("TOT_NOPROC_DOL"));
		objL_garantiaPorLinea.setDblPb_montoUsado(objA_resultSet.getDouble("CRE_USADO_DOL"));
		objL_garantiaPorLinea.setStrPb_nombreLinea(objA_resultSet.getString("LINEA"));
		
		return objL_garantiaPorLinea;
	}

	
}
